package com.jxs.android.sxyw;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.Person;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

public class WorkOrderNextPersonList extends AbstractActivity {

	private String case_no;
	private String workOrderType;
	private ListView listView;
	private PersonAdapter adapter;
	private TextView empytText;
	private List<Person> persons = new ArrayList<Person>();
	private ProgressDialog progressDialog;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.person_list);
		if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
			PopMsg.show("登录已过期,请重新登录");
			StartHelper.startActivity(Login.class);
			return;
		}
		listView = (ListView) findViewById(R.id.common_listview);
		empytText = (TextView) findViewById(R.id.null_has_data);
		empytText.setText("暂无" + getString(R.string.work_order_processer));
		adapter = new PersonAdapter();
		listView.setAdapter(adapter);

		if (null != getIntent() && null != getIntent().getExtras()) {
			case_no = getIntent().getExtras().getString("case_no");
			workOrderType = getIntent().getExtras().getString("workOrderType");
			String methodName = getIntent().getExtras().getString("method_name");
			progressDialog = ProgressDialog.show(this, "", "正在加载，请稍候...", true);
			progressDialog.setCancelable(true);
			getPersonList(methodName, case_no, workOrderType);
		}
	}

	private int GET_NEW = 0x0091;
	private int ERROR_MSG = 0x0092;
	private int LOGIN = 0x0093;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			empytText.setVisibility(View.GONE);
			if (msg.what == GET_NEW) {
				if (persons.size() < 1) {
					empytText.setVisibility(View.VISIBLE);
				}
				adapter.setPersons(persons);
				adapter.notifyDataSetChanged();
			} else if (msg.what == ERROR_MSG) {
				PopMsg.show(msg.obj.toString());
			} else if (msg.what == LOGIN) {
				PopMsg.show("登录已过期,请重新登录");
				StartHelper.startActivity(Login.class);
				finish();
			}
		}

	};

	private void getPersonList(final String methodName, final String caseNo, final String workOrderType) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = SelfApplication.WSDL_URL;// wsdl
				String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace

				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", SelfApplication.getUserInfo().getTicket());
				request.addProperty("case_no", caseNo);
				request.addProperty("workOrderType", workOrderType);
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paser(response);
					}
				});
			}
		}).start();
	}

	private void paser(SoapObject soapObject) {
		if (null != soapObject) {
			String stauts = soapObject.getPropertySafelyAsString("status");
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getPropertySafelyAsString("message");
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				handler.sendEmptyMessage(LOGIN);
			} else if ("0".equals(stauts)) {
				SoapObject retMsgObject = (SoapObject) soapObject.getProperty("retMsg");
				if (null != retMsgObject) {
					persons.clear();
					for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
						SoapObject personBeanObject = (SoapObject) retMsgObject.getProperty(i);
						Person person = new Person();
						person.setUser_id(personBeanObject.getPropertySafelyAsString("user_id"));
						person.setUser_name(personBeanObject.getPropertySafelyAsString("user_name"));
						person.setLogin_name(personBeanObject.getPropertySafelyAsString("login_name"));
						person.setPosition(personBeanObject.getPropertySafelyAsString("position"));
						persons.add(person);
					}
					Message msg = new Message();
					msg.what = GET_NEW;
					handler.sendMessage(msg);
				} else {
					Message msg = new Message();
					msg.what = ERROR_MSG;
					msg.obj = message;
					handler.sendMessage(msg);
				}
			}
		}
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(R.string.work_order_processer);
		initTopBarReturn(topBar);
		topBar.hindBtn();

	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private class PersonAdapter extends BaseAdapter {

		private List<Person> persons = new ArrayList<Person>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public PersonAdapter() {
			inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setPersons(List<Person> persons) {
			this.persons.clear();
			this.persons.addAll(persons);
		}

		public int getCount() {
			return persons.size();
		}

		public Person getItem(int position) {
			return persons.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			final Person person = persons.get(position);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.persons_item, null);
				holder = new ViewHolder();
				holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
				holder.login_name = (TextView) convertView.findViewById(R.id.login_name);
				holder.position_tv = (TextView) convertView.findViewById(R.id.position);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.user_name.setText(person.getUser_name());
			holder.login_name.setText(person.getLogin_name());
			holder.position_tv.setText(person.getPosition());
			OnClickListener onClickListener = new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onclickView(position, person);
				}
			};

			convertView.setOnClickListener(onClickListener);
			if (position == persons.size() - 1) {
				convertView.setBackgroundResource(R.drawable.roundlist_tail);
			} else {
				convertView.setBackgroundResource(R.drawable.roundlist_body);
			}
			return convertView;
		}

		private void onclickView(int position, Person person) {
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putSerializable("person", person);
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finish();
		}

		class ViewHolder {
			TextView user_name;// Index
			TextView login_name;// 昵称
			TextView position_tv;// 状态
		}

	}

}
